#pragma once
#include <cstdint>
#include <linux/spi/spi.h>
#include <span>
#include <string>

namespace AD
{
class Spi
{
  private:
    int fd = -1;
    bool isOpen = false;

  public:
    using SPIMODE = uint8_t;
    // sample at raising, idel is low
    static const SPIMODE MODE0 = SPI_MODE_0;
    // sample at falling, idel is low
    static const SPIMODE MODE1 = SPI_MODE_1;
    // sample at raising, idel is high
    static const SPIMODE MODE2 = SPI_MODE_2;
    // sample at falling, idel is high
    static const SPIMODE MODE3 = SPI_MODE_3;

    ~Spi();
    bool SetSpeed(uint32_t speed);
    bool SetBitWidth(uint8_t width);
    bool SetMode(SPIMODE mode);
    bool Open(const std::string &dev);
    bool IsOpen();
    bool Close();
    bool Send(std::span<const unsigned char> data);
};
} // namespace AD